home *** CD-ROM | disk | FTP | other *** search
/ Aminet 43 / Aminet 43 (2001)(GTI - Schatztruhe)[!][Jun 2001].iso / Aminet / comm / tcp / AmiVNC.lha / AmiVNC / vncauth.c < prev    next >
C/C++ Source or Header  |  2001-02-26  |  3KB  |  129 lines

  1. //  Copyright (C) 1997, 1998 Olivetti & Oracle Research Laboratory
  2. //
  3. //  This file is part of the VNC system.
  4. //
  5. //  The VNC system is free software; you can redistribute it and/or modify
  6. //  it under the terms of the GNU General Public License as published by
  7. //  the Free Software Foundation; either version 2 of the License, or
  8. //  (at your option) any later version.
  9. //
  10. //  This program is distributed in the hope that it will be useful,
  11. //  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. //  GNU General Public License for more details.
  14. //
  15. //  You should have received a copy of the GNU General Public License
  16. //  along with this program; if not, write to the Free Software
  17. //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
  18. //  USA.
  19. //
  20. // If the source code for the VNC system is not available from the place 
  21. // whence you received this file, check http://www.orl.co.uk/vnc or contact
  22. // the authors on vnc@orl.co.uk for information on obtaining it.
  23.  
  24.  
  25. /*
  26.  *  Functions for VNC password management and authentication.
  27.  *
  28.  */
  29.  
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. typedef unsigned long clock_t;
  34. #include <time.h>
  35. #include <sys/types.h>
  36. #include <sys/stat.h>
  37. #include "vncauth.h"
  38. #include "d3des.h"
  39.  
  40. /*
  41.  *   We use a fixed key to store passwords, since we assume that our local
  42.  *   file system is secure but nonetheless don't want to store passwords
  43.  *   as plaintext.
  44.  */
  45.  
  46. unsigned char fixedkey[8] = {23,82,107,6,35,78,88,7};
  47.  
  48. /*
  49.  *   Encrypt a password and store it in a file.
  50.  */
  51. int
  52. vncEncryptPasswd(char *passwd, char *encryptedPasswd)
  53. {
  54.     int i;
  55.  
  56.     /* pad password with nulls */
  57.  
  58.     for (i = 0; i < MAXPWLEN; i++) {
  59.     if (i < strlen(passwd)) {
  60.         encryptedPasswd[i] = passwd[i];
  61.     } else {
  62.         encryptedPasswd[i] = 0;
  63.     }
  64.     }
  65.  
  66.     /* Do encryption in-place - this way we overwrite our copy of the plaintext
  67.        password */
  68.  
  69.     deskey(fixedkey, EN0);
  70.     des(encryptedPasswd, encryptedPasswd);
  71.  
  72.     return 8;
  73. }
  74.  
  75. /*
  76.  *   Decrypt a password.
  77.  */
  78. char *
  79. vncDecryptPasswd(char *intext, char *outtext)
  80. {
  81.  
  82.     deskey(fixedkey, DE1);
  83.     des(intext, outtext);
  84.  
  85.     outtext[MAXPWLEN] = 0;
  86.  
  87.     return (char *)outtext;
  88. }
  89.  
  90. /*
  91.  *   Generate a set of random bytes for use in challenge-response authentication.
  92.  */
  93. void
  94. vncRandomBytes(unsigned char *where) {
  95.   int i;
  96.   unsigned int seed = (unsigned int) time(0);
  97.  
  98.   srand(seed);
  99.   for (i=0; i < CHALLENGESIZE; i++) {
  100.     where[i] = (unsigned char)(rand() & 255);    
  101.   }
  102. }
  103.  
  104. /*
  105.  *   Encrypt some bytes in memory using a password.
  106.  */
  107. void
  108. vncEncryptBytes(unsigned char *where, const char *passwd)
  109. {
  110.     unsigned char key[8];
  111.     int i;
  112.  
  113.     /* key is simply password padded with nulls */
  114.  
  115.     for (i = 0; i < 8; i++) {
  116.     if (i < strlen(passwd)) {
  117.         key[i] = passwd[i];
  118.     } else {
  119.         key[i] = 0;
  120.     }
  121.     }
  122.  
  123.     deskey(key, EN0);
  124.  
  125.     for (i = 0; i < CHALLENGESIZE; i += 8) {
  126.     des(where+i, where+i);
  127.     }
  128. }
  129.